<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/22
 * Time: 10:59.
 */

namespace app\admin\controller;

use app\service\AdverService;
use think\Db;

/**
 * 广告管理.
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class Adverbanner extends Common
{
    /**
     * 构造方法.
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

       // $this->IsLogin();
    }

    /**
     * [Index 列表].
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:58:48
     */
    public function Index()
    {
        if (!input('get.cate')) {
            return $this->fetch('banner_cate');

        } else {
            if ('ajax' != input('get.action')) {

                $this->assign('cate', input('get.cate'));

                return $this->fetch();
            } else {

                if(input('get.cate')==1)
                {
                 $where=['advert_type' => input('get.cate')];
                }else{
                    $where=[['advert_type' ,'>', '1']];
                }
                $data_params = [
                    'where' => $where,
                    'page' => true,
                    'field' => 'id,title,imgurl,start_time,end_time,bizpro_id,bizpro_type,advert_type,show_type',
                    'number' => 10,
                    'table'=>'advert'
                ];
                $re = AdverService::DataList($data_params);
                $re = AdverService::DataDealWith($re,[]);
                $total = AdverService::BannerTotal('advert',['advert_type' => input('get.cate')]);

                return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re['data']];
            }
        }
    }

    /**
     * 广告编辑页面
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月22日15:11:06
     */
    public function Saveinfo()
    {
        // 参数
        $params = $this->data_get;
        // 数据
        $data = [];


        if (!empty($params['id'])) {
            $data_params = [
                'where' => ['id' => $params['id']],
                'page' => false,
                'field' => '*',
                'm' => 0,
                'n' => 1,
                'table'=>'advert'
            ];
            $re = AdverService::DataList($data_params);
            $re = AdverService::DataDealWith($re,[]);

            $data = empty($re['data'][0]) ? [] : $re['data'][0];
        }

        $this->assign('cate', input('get.cate'));
        $this->assign('res', $data);

        if(input('get.cate')==1){
            return $this->fetch('saveinfo_index');
        }else{
            return $this->fetch('saveinfo_usercenter');
        }

    }

    /**
     * @return array
     * @content 查看用户的卡券记录
     */
    public function bizpro()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }

        $re=AdverService::ProductsStr();

        $data = empty($re['data'][0]) ? [] : $re['data'];

        return json(['status'=>true,'info'=>$data]);
    }

    /**
     * [Save 广告添加/编辑].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function Save()
    {

        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }

        // 开始操作
        $params = $this->data_post;

        //数据校验
        AdverService::VaryData($params);

        //整理数据
        $data=AdverService::HandleData($params);

        AdverService::SaveAd($data,$params);

        return DataReturn('保存成功', 0);
    }


    /**
     * 删除广告.
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function DelInfo()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }
        $params = $this->data_post;

        AdverService::DoDel($params);

        return DataReturn('删除成功', 0);
    }

    public function LeegameStatus()
    {

        $params=input();

        $where=[
            ['m.phone' ,'=',$params['phone']],
            ['mo.pay_status' ,'=',1],
            ['mo.add_integral' ,'=',1],
        ];

        $re=Db::name('member_order')
            ->alias('mo')
            ->join(['member'=>'m'], 'm.id=mo.member_id')
//            ->join(['member_orderdetail'=>'mod'], 'mo.id=mod.order_id')
            ->where($where)->field('mo.pay_price,mo.create_time,mo.id order_id')->select();

        //Db::name('member_order')->update(['add_integral'=>2]);
        return json($re);
    }
}
